package com.njym.java.security.common.util;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhy
 * @date 2019-11-18 14:49
 **/
public class AppPush {
	// STEP1：获取应用基本信息
	private static String appId = "tlf5xjAv9Z68hMLRhX7Af8";

	private static String appKey = "otGpXun0og6M0ZnsuG32MA";

	private static String masterSecret = "scqPrqWk8d7HtlnvYnlLj2";

	private static String url = "http://sdk.open.api.igexin.com/apiex.htm";



	//	public static void main(String[] args) throws IOException {
	//		String title = "滚蛋哦";
	//		String text = "寸v打发";
	//		String logo = "https://oss-hong-yi.oss-cn-beijing.aliyuncs.com/20191108/bc77ca158a2244fbb1c18c07d7283655.jpg";
	//		//		pushAll(title,text,logo,"../lesssons/lesson");
	//		pushOne(title, text, logo, "../lesssons/lesson", "4b598ce515a54c5f3f87c6e56635a8c3");
	//
	//	}

	//个推
	public static void pushOne(String title, String content, String logo, String path, String cId) {
		IGtPush push = new IGtPush(url, appKey, masterSecret);
		Target target = new Target();
		target.setAppId(appId);
		target.setClientId(cId);

		TransmissionTemplate template = new TransmissionTemplate();
		template.setAppId(appId);
		template.setAppkey(appKey);
		template.setTransmissionType(1);
		template.setTransmissionContent("{title:\"" + title + "\",content:\"" + content + "\",payload:\"" + path + "\"}");

		SingleMessage message = new SingleMessage();
		message.setData(template);
		message.setOffline(true);
		message.setOfflineExpireTime(1000 * 600);

		List<String> appIds = new ArrayList<>();
		appIds.add(appId);

		IPushResult ret = push.pushMessageToSingle(message, target);
		System.out.println(ret.getResponse().toString());
	}

	//群推
	public static void pushAll(String title, String content, String logo, String path) {
		IGtPush push = new IGtPush(url, appKey, masterSecret);
		TransmissionTemplate t = new TransmissionTemplate();
		t.setAppId(appId);
		t.setAppkey(appKey);
		t.setTransmissionContent("{title:\"" + title + "\",content:\"" + content + "\",payload:\"" + path + "\"}");
		t.setTransmissionType(1);

		List<String> appIds = new ArrayList<>();
		appIds.add(appId);

		// 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
		AppMessage message = new AppMessage();
		message.setData(t);
		message.setAppIdList(appIds);
		message.setOffline(true);
		message.setOfflineExpireTime(1000 * 600);

		IPushResult ret = push.pushMessageToApp(message);
		System.out.println(ret.getResponse().toString());
	}
}
